#coding=utf-8
'''
Created on Dec 10, 2015
@author: DQA wangpeng 
'''
import data.desired_setting

import data.logging_save

import time
from appium import webdriver
import data.config
import data.get_status
import threading
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import data.logging_save

import unittest


class guess(unittest.TestCase):
    def setUp(self):
        #startup the huawei m130 app
        desired_caps = data.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        data.logging_save.logging.info("startup the app already")
        print 'startup the app already'
    def tearDown(self):
        #self.driver.quit()
        pass
        
    def test_alert(self):
        u"""测试步骤：播放虾米里面的“猜你喜欢”专辑，点击10次       测试依据：底部获取到的名字和HTTP获取到的名字是否一致进行判断"""
        #alert times count
        alert_number = 1
        #excute the test case times
        testnumber = 1
        alert_number = 1
        playback_times = 1
        success_times = 1
        fail_times = 1
        while(alert_number <=10 and playback_times <=5):
        #test guess you like 10 times and if the alert_number times is up to 10 tims .the loop will be break
            try:
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.playback_button_songname())))     
                self.driver.find_element_by_xpath(data.config.Get_xiami_guess_you_like()).click()
                el_button_text_1 = self.driver.find_element_by_xpath(data.config.playback_button_songname())
                #check if the songname match with the http
                button_songname = el_button_text_1.text
                #for logging display
                data.logging_save.logging.info("底部播放器获取到的歌曲名字是："+str(button_songname))
                #for htmltestrunner display
                print "底部播放器获取到的歌曲名字是：%s"%button_songname
                get_songname_http = data.get_status.songname()
                time.sleep(2)
                data.logging_save.logging.info("通过网络获取到的歌曲名字是:"+str(get_songname_http))
                print "通过网络获取到的歌曲名字是：%s"%get_songname_http
                time.sleep(5)
                try:
                    self.assertIn(get_songname_http, button_songname)
                    print "经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                    data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                    success_times = success_times + 1
                    
                except:
                    self.fail("the current song name do not match the real songname after click the 'guess you like'")
                
                playback_times  = playback_times + 1
                
            except:
                try:
                    WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.XPATH,"//UIAApplication[1]/UIAWindow[5]/UIAAlert[1]/UIACollectionView[1]/UIACollectionCell[2]")))     
                    alert = self.driver.switch_to_alert()
                    time.sleep(1)
                    alert.accept()
                    data.logging_save.logging.info("弹出烦人的“网络错误”提示框，次数为："+str(alert_number)+"次")
                    print "弹出烦人的“网络错误”提示框，次数为：：%s"%alert_number
                    alert_number = alert_number + 1    
                    time.sleep(3)                
                    if alert_number == 10:
                        data.logging_save.logging.WARNING("the netwrok envirument is too bad")
                        print "the netwrok envirument is too bad"
                        self.assertTrue(self.driver.find_element_by_xpath(data.config.playback_button_songname()),u'一直刷新不出来网络')
                        
                except:
                    print '111'
                    self.assertTrue(self.driver.find_element_by_xpath(data.config.playback_button_songname()),u'一直刷新不出来网络')
                   
if __name__ == '__main__':
    unittest.main()